package core

import (
	"fmt"
	"ginvueadmin/databases"
	"ginvueadmin/global"
	"ginvueadmin/routers"
	"time"

	"github.com/fvbock/endless"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type server interface {
	ListenAndServe() error
}

func initServer(addr string, router *gin.Engine) server {
	s := endless.NewServer(addr, router)
	s.ReadHeaderTimeout = 10 * time.Millisecond
	s.WriteTimeout = 10 * time.Second
	s.MaxHeaderBytes = 1 << 20

	return s
}

func RunWinServer() {
	// init redis serve
	if global.CONFIG.System.UseMultipoint {
		databases.Redis()
	}

	Router := routers.Routers()

	// 静态文件
	Router.Static("/form-generator", "./resource/page")

	address := fmt.Sprintf(":%d", global.CONFIG.System.Addr)
	s := initServer(address, Router)

	time.Sleep(10 * time.Microsecond)
	global.LOG.Info("server run success on ", zap.String("address", address))

	fmt.Printf("start: 127.0.0.1%s \n", address)
	global.LOG.Error(s.ListenAndServe().Error())
}
